#include "Connection.h"
#include "BasePort.h"
#include <QStyleOptionGraphicsItem>
#include <QGraphicsSceneMouseEvent>
#include <QCursor>
#include <QGraphicsScene>
#include <QPainterPath>
#include <QPainter>
#include <QColor>
#include <QVariant>
#include <QLineF>
#include <QtMath>

Connection::Connection(BasePort* startPort, BasePort* endPort)
    : m_startPort(startPort)
    , m_endPort(endPort)
    , m_color(QColor(150, 150, 200))
    , m_width(2.0)
{
    // 设置连接线可以接收鼠标事件
    setAcceptHoverEvents(true);
    setAcceptedMouseButtons(Qt::LeftButton);
    
    // 设置连接线的Z值，使其显示在节点下方
    setZValue(-1);
    
    // 更新路径
    updatePath();
    
    // 如果端口存在，添加到端口的连接列表中
    if (m_startPort) {
        m_startPort->addConnection(this);
    }
    
    if (m_endPort) {
        m_endPort->addConnection(this);
    }
}

Connection::~Connection()
{
    // 从端口的连接列表中移除
    if (m_startPort) {
        m_startPort->removeConnection(this);
    }
    
    if (m_endPort) {
        m_endPort->removeConnection(this);
    }
}

QRectF Connection::boundingRect() const
{
    // 返回连接线的边界矩形，增加一些边距
    qreal extra = m_width + 10;
    return m_path.boundingRect().adjusted(-extra, -extra, extra, extra);
}

void Connection::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)
    
    // 设置抗锯齿渲染
    painter->setRenderHint(QPainter::Antialiasing);
    
    // 绘制连接线
    QPen pen(m_color, m_width);
    pen.setStyle(Qt::SolidLine);
    pen.setCapStyle(Qt::RoundCap);
    pen.setJoinStyle(Qt::RoundJoin);
    painter->setPen(pen);
    
    painter->drawPath(m_path);
    
    // 绘制箭头
    if (!m_path.isEmpty()) {
        // 计算箭头位置
        QPainterPath arrowPath;
        QPointF endPoint = m_path.currentPosition();
        
        // 绘制箭头（简单三角形）
        QPolygonF arrowHead;
        arrowHead << endPoint
                  << QPointF(endPoint.x() - 10, endPoint.y() - 5)
                  << QPointF(endPoint.x() - 10, endPoint.y() + 5);
        
        painter->setBrush(QBrush(m_color));
        painter->drawPolygon(arrowHead);
    }
}

void Connection::transmitData()
{
    // 从起始端口传输数据到结束端口
    if (m_startPort && m_endPort && m_startPort->hasData()) {
        QVariant data = m_startPort->getData();
        m_endPort->setData(data);
    }
}

void Connection::updatePath()
{
    // 获取端口位置
    if (m_startPort) {
        m_startPoint = m_startPort->scenePos();
    }
    
    if (m_endPort) {
        m_endPoint = m_endPort->scenePos();
    }
    
    // 创建贝塞尔曲线路径
    m_path = QPainterPath();
    m_path.moveTo(m_startPoint);
    
    // 计算控制点，使连接线更美观
    qreal dx = m_endPoint.x() - m_startPoint.x();
    qreal dy = m_endPoint.y() - m_startPoint.y();
    
    QPointF ctrl1(m_startPoint.x() + dx * 0.25, m_startPoint.y());
    QPointF ctrl2(m_endPoint.x() - dx * 0.25, m_endPoint.y());
    
    m_path.cubicTo(ctrl1, ctrl2, m_endPoint);
    
    // 更新边界矩形
    prepareGeometryChange();
}

void Connection::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    // 处理鼠标按下事件
    if (event->button() == Qt::LeftButton) {
        // 可以在这里添加选中效果
        m_color = QColor(200, 200, 255);
        update();
    }
    
    QGraphicsItem::mousePressEvent(event);
}

void Connection::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    // 处理鼠标释放事件
    if (event->button() == Qt::LeftButton) {
        // 恢复原始颜色
        m_color = QColor(150, 150, 200);
        update();
    }
    
    QGraphicsItem::mouseReleaseEvent(event);
}